﻿@using KRM.Core
@using KRM.Core.General
@model IEnumerable<KRM.Core.vWebListPost>
@{
    ViewBag.Title = "Espresso News Feed";
    tblUser user = ViewBag.User;
    var authen = User.Identity.IsAuthenticated;
    Layout = !authen ? "~/Views/Shared/_NewsFeedNonUserLayout.cshtml" : "~/Views/Shared/_Layout.cshtml";
    KRMEntities db = ViewBag.db;
    var pagecount = ViewBag.PagingCount;
    var page = ViewBag.Page;
}
<script src="~/js/jquery.textarea_autosize.js"></script>
<script src="~/js/post.js"></script>
<div class="container-fluid newsfeed">
    <div class="page-header"></div>
    <div class="row-fluid">
        <div class="span@(!authen ? 12: 8)">
            @if (!authen)
            {
                <h1 class="newsfeedtitle">
                    <a href="/" title="Espresso News Feed">Espresso News Feed</a>
                </h1>
            }
            else
            {
                <ul class="tabs tabs-inline tabs-top newsfeedtabs">
                    <li class="active">
                        <a href="#statusupdate" data-toggle="tab"><i class="icon-comment"></i>Status Update</a>
                    </li>
                    <li>
                        <a href="#images" data-toggle="tab"><i class="icon-picture"></i>Images</a>
                    </li>
                    <li>
                        <a href="#quote" data-toggle="tab"><i class="icon-quote-left"></i>Quote<i class="icon-quote-right"></i></a>
                    </li>
                    <li>
                        <a href="#blogpost" data-toggle="tab"><i class="icon-edit"></i>Blog Post</a>
                    </li>
                </ul>

                <div class="tab-content tab-content-inline tab-content-bottom newsfeedtabs-content">
                    <div class="tab-pane active" id="statusupdate">
                        <form class="margin0 fr-postnewstatus" method="POST" action="/post/postnew">
                            <input type="hidden" name="posttypeid" value="1" />
                            <div class="row-fluid">
                                <textarea class="input-block-level boxcomment" name="content" rows="4" placeholder="What's on your mind..." required="required"></textarea>
                            </div>
                            <div class="text-right">
                                <input type="submit" class="bold btn btn-primary w100" value="Post it" />
                            </div>
                        </form>
                    </div>
                    <div class="tab-pane" id="images">
                        <form class="margin0 fr-postnewstatus" method="POST" action="/post/postnew" enctype="multipart/form-data">
                            <input type="hidden" name="posttypeid" value="2" />
                            <div class="row-fluid">
                                <input type="file" name="image" style="line-height: 20px" required="required" />
                            </div>
                            <div class="row-fluid">
                                <textarea class="input-block-level boxcomment" rows="3" name="content" placeholder="Say something about this image..."></textarea>
                            </div>
                            <div class="text-right">
                                <input type="submit" class="bold btn btn-primary w100" value="Post it" />
                            </div>
                        </form>
                    </div>
                    <div class="tab-pane" id="quote">
                        <form class="margin0 fr-postnewstatus" method="POST" action="/post/postnew">
                            <input type="hidden" name="posttypeid" value="3" />
                            <div class="row-fluid">
                                <textarea class="input-block-level boxcomment" rows="4" name="content" placeholder="Quote..." required="required"></textarea>
                            </div>
                            <div class="row-fluid">
                                <input type="text" name="title" class="input-block-level" placeholder="Citation" required="required" />
                            </div>
                            <div class="text-right">
                                <input type="submit" class="bold btn btn-primary w100" value="Post it" />
                            </div>
                        </form>
                    </div>
                    <div class="tab-pane" id="blogpost">
                        <form class="margin0 fr-postnewstatus" method="POST" action="/post/postnew">
                            <input type="hidden" name="posttypeid" value="4" />
                            <div class="row-fluid">
                                <input type="text" name="title" class="input-block-level" placeholder="Post Title" required="required" />
                            </div>
                            <div class="row-fluid">
                                <textarea class="input-block-level boxcomment" name="content" rows="4" required="required"></textarea>
                            </div>
                            <div class="text-right">
                                <input type="submit" class="bold btn btn-primary w100" value="Post it" />
                            </div>
                        </form>
                    </div>
                </div>
            }


            <div class="listpost">
                @foreach (var item in Model)
                {
                    <div class="postitem">
                        <div class="postavata">
                            <a href="/?author=@item.PostByUserId" title="@item.Username">
                                <img src="/Upload/Avatar/@(item.Avatar ?? "user-default.png")" onerror="this.src='/upload/avatar/user-default.png'" alt="@item.Username"/>
                            </a>
                        </div>
                        <div class="postsummary">
                            <p class="postauthor">
                                <a href="/?author=@item.PostByUserId" title="@item.Username" class="bold">@item.Username</a>
                                <span class="postdate">@string.Format("{0: HH:mm tt on MMMM d, yyyy}", item.PostDate)</span>
                            </p>
                            <div class="postcontent">
                                @switch (item.PostTypeId)
                                {
                                    case 1:
                                    @Html.Raw(item.Content)
                                        break;
                                    case 2:
                                        if (!string.IsNullOrEmpty(item.Content))
                                        {
                                    @Html.Raw(item.Content)
                                        }
                                        foreach (var image in db.tblKRMWPostImages.Where(a => a.PostId == item.PostId))
                                        {
                                    <a href="@image.Url" target="_blank">
                                        <img src="@image.Url" alt="" style="margin-top: 10px"/>
                                    </a>
                                        }
                                        break;
                                    case 3:
                                    <blockquote>
                                        <p>@Html.Raw(item.Content)</p>
                                        <small>@item.Title</small>
                                    </blockquote>
                                        break;
                                    case 4:
                                    <h3 style="font-weight: 300">
                                        <a href="/post/view/@item.PostId" title="@item.Title">@item.Title</a>
                                    </h3>
                                    @Html.Raw(item.Content.Replace("\r\n", "<br />"))
                                        break;
                                }
                            </div>
                            <div class="postaction">
                                <a href="javascript:;" title="Comment" onclick="@(!authen ? "alert('Please login to comment!');" : "$('.postcomment" + item.PostId + "').show(); $('.postcomment" + item.PostId + " textarea').focus()")">Comment</a>
                                <a href="/post/view/@item.PostId" title="Permalink">Permalink</a>
                                <span>@General.TimeAgo(item.PostDate.To<DateTime>())</span>
                            </div>
                            <div class="postcomment postcomment@(item.PostId)" @(item.CommentCount > 0 ? "" : "style=display:none")>
                                <div class="arrowtop"></div>
                                <div class="listcomment listcomment@(item.PostId)">
                                    @if (item.CommentCount > 0)
                                    {
                                        var comments = item.ListComments;
                                        if (comments.Count() > 3)
                                        {
                                        <div class="readmorecomment">
                                            <a href="javascript:;" onclick="seeallcomments(@item.PostId)">See more @comments.Skip(3).Count() other comments
                                            </a>
                                        </div>
                                        }
                                        foreach (var comment in comments.Take(3))
                                        {
                                            var userComment = comment.User;
                                        <div class="formcomment commentitem">
                                            <div class="commentavatar">
                                                <a href="/?author=@comment.CommentByUserId" title="@userComment.Username">
                                                    <img src="/Upload/Avatar/@(userComment.Avatar ?? "user-default.png")" onerror="this.src='/upload/avatar/user-default.png'" alt="@userComment.Username"/>
                                                </a>
                                            </div>
                                            <div class="commentcontent">
                                                <div>
                                                    <a href="/?author=@comment.CommentByUserId" title="@userComment.Username" class="bold" style="margin-right: 8px">@userComment.Username</a>
                                                    <span class="timeago">@General.TimeAgo(comment.CommentDate.To<DateTime>())</span>
                                                    @if (authen && user.UserID == comment.CommentByUserId)
                                                    {
                                                        <div class="deletecomment" onclick="deletecomment('@comment.CommentId', '@comment.PostId')" title="Delete comment"></div>
                                                    }
                                                </div>
                                                <div class="commenttext">
                                                    @Html.Raw(comment.Content.Replace("\r\n", "<br />"))
                                                </div>
                                            </div>
                                        </div>
                                        }
                                    }
                                </div>
                                @if (authen)
                                {
                                    <form class="margin0 fr-comment fr-comment@(item.PostId)" method="POST" data-id="@item.PostId">
                                        <input type="hidden" name="postid" value="@item.PostId"/>
                                        <div class="formcomment">
                                            <div class="commentavatar">
                                                <a href="?author=@user.UserID" title="@user.Username">
                                                    <img src="/Upload/Avatar/@(user.Avatar ?? "user-default.png")" onerror="this.src='/upload/avatar/user-default.png'" alt="@user.Username"/>
                                                </a>
                                            </div>
                                            <div class="commentcontent">
                                                <textarea class="input-block-level boxcomment" id="areacomment@(item.PostId)" style="height: 40px" name="content"
                                                      onkeydown="if (event.keyCode == 13 && !event.shiftKey) $('.fr-comment@(item.PostId)').submit();"></textarea>
                                            </div>
                                        </div>
                                        <span class="tip">(Press Shift + Enter for newline)</span>
                                    </form>
                                }
                            </div>
                        </div>
                    </div>
                }
            </div>
            @if (pagecount > 1)
            {
                <div class="newsfeedpaging">
                    @if (pagecount > page)
                    {
                        <div class="pull-left">
                            <a href="/?p=@(page + 1)" type="Older Posts">← Older Posts</a>
                        </div>
                    }
                    @if (page > 1)
                    {
                        <div class="pull-right">
                            <a href="/?p=@(page - 1)" type="Newer Posts">Newer Posts →</a>
                        </div>
                    }
                </div>
            }
        </div>
    </div>
</div>
